#include "timpwm.h"
#include "ui_timpwm.h"
#include "control.h"
#include  "QDebug"
#include "string"

int stop = 0;

TIMPWM::TIMPWM(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::TIMPWM)
{
    ui->setupUi(this);

    connect(this, SIGNAL(ccr_change()), this, SLOT(ccr_play()));

    connect(this, SIGNAL(count_change()), this, SLOT(ccr_play()));

    setWindowFlag(Qt::FramelessWindowHint);
}

TIMPWM::~TIMPWM()
{
    delete ui;
}

void TIMPWM::ccr_play()
{

    extern QSerialPort *serial;
    extern QTcpSocket *qtcpsocket;

    extern int tcp_state;
    char ccr_value[10];

    sprintf(ccr_value,"%d",ui->CCR->value());
    if(serial->isOpen()==true)
    {
        serial->write("@");
        serial->write("CCR");
        serial->write(ccr_value);
        serial->write(" ");
        serial->write(".");
    }

    if(tcp_state == 1)
    {
        qtcpsocket->write("@");
        qtcpsocket->write("CCR");
        qtcpsocket->write(ccr_value);
        qtcpsocket->write(" ");
        qtcpsocket->write(".");
    }

}

void TIMPWM::count_play()
{
    extern QByteArray TCP_buff;
    extern QByteArray getcmd;
    char* count_play = NULL;
    char buff[100];

    extern QSerialPort *serial;
    extern int tcp_state;

    if(serial->isOpen()==true)
    {
        count_play = strstr(getcmd, "COUNT");
        if(count_play != NULL)
        {
           count_play+=5;
           sprintf(buff,"%d",atoi(count_play));
          ui->count->setText(buff);
        }
    }
    if(tcp_state == 1)
    {
        count_play = strstr(TCP_buff, "COUNT");
        if(count_play != NULL)
        {
            count_play+=5;
            sprintf(buff,"%d",atoi(count_play));
           ui->count->setText(buff);
        }
    }

}

void TIMPWM::on_pushButton_2_clicked()
{
  extern QSerialPort *serial;
  if(serial->isOpen()==true)
  {
    serial->write("@");
    serial->write(ui->TIMx->currentText().toUtf8());
    serial->write("_");
    serial->write("PSC");
    serial->write(ui->PSC->text().toUtf8());
    serial->write("_");
    serial->write("ARR");
    serial->write(ui->ARR->text().toUtf8());
    serial->write("_");
    serial->write("REC");
    serial->write(ui->REC->text().toUtf8());
    serial->write("_");
    if(ui->PWM->isChecked()==false)
    {

         serial->write("IT");
         serial->write(ui->IT->currentText().toUtf8());
         serial->write("_");

    }
    else

    {

        serial->write("PWM");
        serial->write("_");

        serial->write("CHANNEL");
        serial->write(ui->channel->currentText().toUtf8());
        serial->write("_");



        serial->write(" ");
         serial->write(".");







    }
   }


  extern int tcp_state;
  extern QTcpSocket *qtcpsocket;
  if(tcp_state == 1)
  {
    qtcpsocket->write("@");
    qtcpsocket->write(ui->TIMx->currentText().toUtf8());
    qtcpsocket->write("_");
    qtcpsocket->write("PSC");
    qtcpsocket->write(ui->PSC->text().toUtf8());
    qtcpsocket->write("_");
    qtcpsocket->write("ARR");
    qtcpsocket->write(ui->ARR->text().toUtf8());
    qtcpsocket->write("_");

    qtcpsocket->write("REC");
    qtcpsocket->write(ui->REC->text().toUtf8());
    qtcpsocket->write("_");

    if(ui->PWM->isChecked()==false)
    {
        qtcpsocket->write("IT");
        qtcpsocket->write(ui->IT->currentText().toUtf8());
        qtcpsocket->write("_");
    }
    else
    {
        qtcpsocket->write("PWM");
        qtcpsocket->write("_");
        qtcpsocket->write("CHANNEL");
        qtcpsocket->write(ui->channel->currentText().toUtf8());
        qtcpsocket->write("_");
        qtcpsocket->write(" ");
        qtcpsocket->write(".");


    }

}


}

void TIMPWM::on_pushButton_clicked()
{
    extern QSerialPort *serial;
    extern QTcpSocket *qtcpsocket;
    extern int tcp_state;


    int i;
       for(i=0;i<5;i++)
       {

           if(serial->isOpen()==true)
           {
               serial->write("@");
               serial->write("close");
               serial->write(" ");
               serial->write(".");
           }

       }


       if(tcp_state == 1)
       {
           qtcpsocket->write("@");
           qtcpsocket->write("close");
           qtcpsocket->write(" ");
           qtcpsocket->write(".");
       }
       this->close();
}


void TIMPWM::on_CCR_valueChanged(int value)
{
    if(value)
    {


    }
    emit ccr_change();
}



void TIMPWM::on_pushButton_4_clicked()
{
    extern QSerialPort *serial;
    extern QTcpSocket *qtcpsocket;
        extern int tcp_state;



        if(serial->isOpen()==true)
        {
            serial->write("@");
            serial->write("stop");
            serial->write(" ");
            serial->write(".");
        }

    if(tcp_state == 1)
    {
        qtcpsocket->write("@");
        qtcpsocket->write("stop");
        qtcpsocket->write(" ");
        qtcpsocket->write(".");
    }

}



void TIMPWM::on_pushButton_5_clicked()
{
    extern QSerialPort *serial;
    extern QTcpSocket *qtcpsocket;
        extern int tcp_state;


        if(serial->isOpen()==true)
        {
            serial->write("@");
            serial->write("zero");
            serial->write(" ");
            serial->write(".");
        }




    if(tcp_state == 1)
    {
        qtcpsocket->write("@");
        qtcpsocket->write("zero");
        qtcpsocket->write(" ");
        qtcpsocket->write(".");
    }

}

void TIMPWM::on_pushButton_3_clicked()
{
    extern QSerialPort *serial;
    extern QTcpSocket *qtcpsocket;
        extern int tcp_state;

        if(serial->isOpen()==true)
        {
            serial->write("@");
            serial->write("start");
            serial->write(" ");
            serial->write(".");




        }




        if(tcp_state == 1)
        {
        qtcpsocket->write("@");
        qtcpsocket->write("start");
        qtcpsocket->write(" ");
        qtcpsocket->write(".");



        }
}
